﻿using System;
using Sustainalytics.Utils;
using Sustainalytics.Utils.WebApi.ErrorCodes;
using Sustainalytics.Entities;
using System.Web.Http;
using Sustainalytics.FTPReport.BusinessLogic;
using System.Net.Http;
using System.Net;
using System.Web.Http.Cors;
using Sustainalytics.Utils.Serilog;

namespace Sustainalytics.FTPReports.Service
{
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    [RoutePrefix("api/v1/ftpaccountinfo")]
    public class FtpAccountInfoController : ApiController, IFtpAccountInfoController
    {

        IReportsFtpManagement businessLogicReportsFtp;

        public FtpAccountInfoController()
        {
            try
            {
                CommonEventSerilog.Log.InitController();

                businessLogicReportsFtp = new ReportsFtpManagement();

                CommonEventSerilog.Log.InitControllerSuccess();
            }
            catch (Exception ex)
            {
                CommonEventSerilog.Log.InitControllerError(ex);
            }
        }

        public FtpAccountInfoController(IReportsFtpManagement reportsFtpManager)
        {
            try
            {
                CommonEventSerilog.Log.InitController();

                businessLogicReportsFtp = reportsFtpManager;

                CommonEventSerilog.Log.InitControllerSuccess();
            }
            catch (Exception ex)
            {
                CommonEventSerilog.Log.InitControllerError(ex);
            }
        }

        [Route("account/{accountId}")]
        [HttpGet]
        public HttpResponseMessage Get(Guid accountId)
        {
            try
            {
                if (accountId == null)
                {

                    return Request.CreateResponse(HttpStatusCode.BadRequest,
                    new { ErrorStatusCode = WebAPIGeneralErrorCodes.OperationNotAllowed });
                }


                ReportFTP ftpReportEntity = businessLogicReportsFtp.LoadReportFtpForAccountId(accountId);
                if (ftpReportEntity != null)
                {
                    ReportFTPDto ftpClientReportsDto = ReportFTPDto.ToReportFTPDto(ftpReportEntity);


                    return Request.CreateResponse(HttpStatusCode.OK, ftpClientReportsDto);
                }

                return Request.CreateResponse(HttpStatusCode.NoContent, new { ErrorStatusCode = WebAPIGeneralErrorCodes.ResourceNotFound });
            }
            catch (Exception ex)
            {
                return this.Request.CreateResponse(HttpStatusCode.InternalServerError,
                    new { ErrorStatusCode = WebAPIGeneralErrorCodes.UnhandledException });
            }
        }

        [Route("")]
        [HttpPost]
        public HttpResponseMessage Post([FromBody] ReportFTPDto ftpReportDto)
        {
            try
            {
                if (ftpReportDto == null)
                {
                    var ftpEx = new FtpReportDataObjectMissingException("FTP Report data object is null.");
                    return Request.CreateResponse(HttpStatusCode.MethodNotAllowed,
                                            new { ErrorStatusCode = WebAPIGeneralErrorCodes.InputContentDataMissing });
                }

                businessLogicReportsFtp.CreateReportFtp(ftpReportDto);

                return Request.CreateResponse(HttpStatusCode.OK);

            }
            catch (FtpReportServerSettingsMissingException clEx)
            {
                return Request.CreateResponse(HttpStatusCode.MethodNotAllowed,
                    new
                    {
                        ErrorStatusCode = WebAPIGeneralErrorCodes.OperationFailed,
                        message = clEx.Message
                    });
            }
            catch (FtpReportCriticalPropertiesMissingException ex)
            {
                return Request.CreateResponse(HttpStatusCode.MethodNotAllowed,
                    new
                    {
                        ErrorStatusCode = WebAPIGeneralErrorCodes.InputContentDataMissing,
                        message = ex.Message
                    });
            }
            catch (FtpReportServerCredentialsMissingException ftpEx)
            {
                return Request.CreateResponse(HttpStatusCode.MethodNotAllowed,
                    new
                    {
                        ErrorStatusCode = WebAPIGeneralErrorCodes.UserNotAuthorized,
                        message = ftpEx.Message
                    });
            }
            catch (FtpReportDataObjectMissingException ftpEx)
            {
                return Request.CreateResponse(HttpStatusCode.MethodNotAllowed,
                    new
                    {
                        ErrorStatusCode = WebAPIGeneralErrorCodes.InputIsNull,
                        message = ftpEx.Message
                    });
            }
            catch (Exception normalEx)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError,
                                            new { ErrorStatusCode = WebAPIGeneralErrorCodes.UnhandledException });
            }
        }


        [Route("")]
        [HttpPut]
        public HttpResponseMessage Put([FromBody] ReportFTPDto ftpReportDto)
        {
            try
            {
                if (ftpReportDto == null)
                {
                    var ftpEx = new FtpReportDataObjectMissingException("FTP Report data object is null.");
                    return Request.CreateResponse(HttpStatusCode.MethodNotAllowed,
                                            new { ErrorStatusCode = WebAPIGeneralErrorCodes.InputContentDataMissing });
                }

                businessLogicReportsFtp.UpdateReportFtp(ftpReportDto);

                return Request.CreateResponse(HttpStatusCode.OK);

            }
            catch (FtpReportServerSettingsMissingException clEx)
            {
                return Request.CreateResponse(HttpStatusCode.MethodNotAllowed,
                    new
                    {
                        ErrorStatusCode = WebAPIGeneralErrorCodes.OperationFailed,
                        message = clEx.Message
                    });
            }
            catch (FtpReportCriticalPropertiesMissingException ex)
            {
                return Request.CreateResponse(HttpStatusCode.MethodNotAllowed,
                    new
                    {
                        ErrorStatusCode = WebAPIGeneralErrorCodes.InputContentDataMissing,
                        message = ex.Message
                    });
            }
            catch (FtpReportServerCredentialsMissingException ftpEx)
            {
                return Request.CreateResponse(HttpStatusCode.MethodNotAllowed,
                    new
                    {
                        ErrorStatusCode = WebAPIGeneralErrorCodes.UserNotAuthorized,
                        message = ftpEx.Message
                    });
            }
            catch (FtpReportDataObjectMissingException ftpEx)
            {
                return Request.CreateResponse(HttpStatusCode.MethodNotAllowed,
                    new
                    {
                        ErrorStatusCode = WebAPIGeneralErrorCodes.InputIsNull,
                        message = ftpEx.Message
                    });
            }
            catch (Exception normalEx)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError,
                                            new { ErrorStatusCode = WebAPIGeneralErrorCodes.UnhandledException });
            }
        }

        [Route("reportId/{reportId}")]
        [HttpDelete]
        public HttpResponseMessage Delete(Guid reportId)
        {
            try
            {
                if (reportId == null)
                {
                    var ftpEx = new FtpReportCriticalPropertiesMissingException("Missing critical property of FTP Report. Report Id is null");
                    return Request.CreateResponse(HttpStatusCode.MethodNotAllowed,
                                            new { ErrorStatusCode = WebAPIGeneralErrorCodes.InputContentDataMissing });
                }

                businessLogicReportsFtp.DeleteReportFtp(reportId);

                return Request.CreateResponse(HttpStatusCode.OK);

            }
            catch (FtpReportServerSettingsMissingException clEx)
            {
                return Request.CreateResponse(HttpStatusCode.MethodNotAllowed,
                    new
                    {
                        ErrorStatusCode = WebAPIGeneralErrorCodes.OperationFailed,
                        message = clEx.Message
                    });
            }
            catch (FtpReportCriticalPropertiesMissingException ex)
            {
                return Request.CreateResponse(HttpStatusCode.MethodNotAllowed,
                    new
                    {
                        ErrorStatusCode = WebAPIGeneralErrorCodes.InputContentDataMissing,
                        message = ex.Message
                    });
            }
            catch (FtpReportServerCredentialsMissingException ftpEx)
            {
                return Request.CreateResponse(HttpStatusCode.MethodNotAllowed,
                    new
                    {
                        ErrorStatusCode = WebAPIGeneralErrorCodes.UserNotAuthorized,
                        message = ftpEx.Message
                    });
            }
            catch (FtpReportDataObjectMissingException ftpEx)
            {
                return Request.CreateResponse(HttpStatusCode.MethodNotAllowed,
                    new
                    {
                        ErrorStatusCode = WebAPIGeneralErrorCodes.InputIsNull,
                        message = ftpEx.Message
                    });
            }
            catch (Exception normalEx)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError,
                                            new { ErrorStatusCode = WebAPIGeneralErrorCodes.UnhandledException });
            }
        }
    }
}
